#pragma once
#include "timerchannel.h"
#include <sys/timerfd.h>
#include "../eventloop.h"
namespace TinyRPC{

int create_timerfd(){
    int rt = timerfd_create(CLOCK_REALTIME, 0);
    return rt;
}
TimerChannel::TimerChannel()
: AbstractChannel(create_timerfd())
{
    ::fcntl(m_fd->fd(), F_SETFL, O_NONBLOCK);
}

void TimerChannel::setEventLoop(EventLoop* loop){
    m_ownerLoop = loop;
}
void TimerChannel::read(){
    uint64_t howmany;
    ssize_t n = ::read(m_fd->fd(), &howmany, sizeof howmany);
}

void TimerChannel::setTime( TimeStamp time){
    itimerspec new_value;
    itimerspec old_value;

    new_value.it_value.tv_sec = time.m_microSecSinceEpoch/TimeStamp::kMicroSecPerSecond;
    new_value.it_value.tv_nsec = (time.m_microSecSinceEpoch % TimeStamp::kMicroSecPerSecond)*1000;
    new_value.it_interval.tv_sec = 0;
    new_value.it_interval.tv_nsec = 0;
    timerfd_settime(m_fd->fd(), TFD_TIMER_ABSTIME, &new_value, &old_value);
}

}
